/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.core;

import java.awt.Robot;

import com.bluestone.util.Util;

/**
 * An action is an object having a graphical behavior on the screen. Examples of actions are the ClickAction,DelayAction and MoveAction.
 * The Action class is the abstract superclass of graphical behavior.
 * 
 * @author <a href="mailto:bluestone.daniel.q@gmail.com">daniel.q</a>
 * 
 */
public abstract class Action implements IAction{

	protected Robot robot = Util.getRobot();

	protected String type = null;

	protected String target = null;

	protected String value = null;

	protected String delay = null;

	protected String offset = null;

	protected Object parent;
	
	protected String timeout;
	
	protected String owner;
	
	protected String forward;
	
	protected String id;
	
	protected boolean proxy = false;
	
	/**
	 * 
	 * @param type String that defines the type of the action.
	 * @param target String that defines the graphical elements operated by action.
	 * @param value String that defines the value of the action.
	 * @param delay String that defines the delay time after the action finished.
	 * @param offset String that defines the offset of the graphical elements.
	 */
	public Action(String type, String target, String value, String delay,
			String offset) {
		super();
		this.type = type;
		this.target = target;
		this.value = value;
		this.delay = delay;
		this.offset = offset;
		this.id = Util.generateActionId();
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
 
    public boolean isValid(){
		if(owner!=null && owner.length()>0){
			boolean isfind = false;
			String [] values = owner.split(",");
			String property = Util.getCurrentProject().getProperty().getValue();
			if(property == null){
				Util.warn("invalid "+ toString());
				return false;
			}
			for(int i=0; i< values.length; i++){
				if(property.toUpperCase().indexOf(values[i].toUpperCase())>=0){
					return true;
				}
			}
			Util.warn("invalid "+ toString());
			return false;
		}
		return true;
    }
    /**
     * Abstract implements of the IAction.execute().
     * @return false if the operator of the action is null.
     */
	public boolean execute(){		
		if(robot == null){
			return false;
		}	
		return true;
	}

	@Override
	public String toString() {
		String str = "action:";
		if (type != null) {
			str = str + "type=" + type;
		}
		if (target != null && target.length() > 0) {
			str = str + ",target=" + target;
		}
		if (value != null && value.length() > 0) {
			str = str + ",value=" + value;
		}
		if (delay != null && delay.length() > 0) {
			str = str + ",delay=" + delay;
		}
		if (offset != null && offset.length() > 0) {
			str = str + ",offset=" + offset;
		}
		if (timeout != null && timeout.length() > 0) {
			str = str + ",timeout=" + timeout;
		}
		if (owner != null && owner.length() > 0) {
			str = str + ",owner=" + owner;
		}
		str = str + ",proxy=" + proxy;
		return (str);
	}

	/**
	 * 
	 * @return robot that defines the action operator 
	 */
	public java.awt.Robot getRobot() {
		return robot;
	}

	public void setRobot(java.awt.Robot robot) {
		this.robot = robot;
	}

	public Object getParent() {
		return parent;
	}

	public void setParent(Object parent) {
		this.parent = parent;
	}

	public String getDelay() {
		return delay;
	}

	public void setDelay(String delay) {
		this.delay = delay;
	}

	public String getOffset() {
		return offset;
	}

	public void setOffset(String offset) {
		this.offset = offset;
	}

	/**
	 * If the action exectue time go beyond the timeout, the action return false.
	 * @return timeout 
	 */
	public String getTimeout() {
		return timeout;
	}

	public void setTimeout(String timeout) {
		this.timeout = timeout;
	}
	/**
	 * Owner of the action. For example, owner="ie7", that indicate the action can execute only under the IE7. 
	 * @return owner that defines the owner of the action.
	 */
	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public String getId(){
		return this.id;
	}

	public boolean getProxy() {
		return proxy;
	}

	public void setProxy(boolean proxy) {
		this.proxy = proxy;
	}
}
